// pages/washing/washing.js
const app=getApp();
var util = require('../../utils/util.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    curDevId: '',//设备id
    isLy:true,
    //存储计时器
    setInter: '',
    animation:"",//动画
    as101:1,//开关键
    as102:0,//模式
    imgSrc:"../../images/circle_nor.png",
    activeClass:1,
    tempType:'淋浴温度', //淋浴
    tempVal:47,//温度值（淋浴）
    cnTempType: '采暖温度', //采暖
    cnTempVal: 47,//温度值（采暖）
    isFD:true,//是否防冻中
    modeVal:"夏季",//模式值
    cnType:"散热片",//采暖方式，散热片，地暖
    yali:200,//压力值
    tempMin:30,//温度最小值
    tempMax:60,//采暖温度：散热片80，地暖60
    setTempVal:45,//设置温度值
    setCnTempVal:32,//设置采暖温度
    powerImg: "../../images/power_nor.png",// "../../images/power_sel.png","../../images/power_dis.png",//开关键图标
    modeImg:'../../images/summer_nor.png',//模式开关键
    isWater:false,//是否有水流
    isSb:false,//水泵是否运行
    isWind:false,//风机是否启动
    isFire:false,//是否有火焰
    isPower:true,//开关机
    isECO:false,//eco按键
    isSummer:false,//是否夏季
    setMode:false,//设置模式
    offLine: false, //是否离线
    isLyTemp: false,
    isCnTemp: false,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  imageOnLoad(ev) {
    console.log(`图片加载成功，width: ${ev.detail.width}; height: ${ev.detail.height}`)
  },
  imageOnLoadError() {
    console.log('图片加载失败')
  },
  onLoad: function (options) {
    console.log(options);
    wx.setNavigationBarTitle({
      title: options.curname
    })
    this.setData({
      curDevId: options.deviceId,
      offLine: options.online
    })
    if (options.online) {
      this.devieStatus();
    }
    this.devListFun();//获取设备列表
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  // l蓝牙或是WiFi获取设备状态值
  devieStatus:function(){
    var that=this;
    var timestamp = new Date().getTime();
      wx.request({
        url: 'https://papi.zcyun.cn/v1/device/snapshots/' + that.data.curDevId,
        data: {
          "did": that.data.curDevId
        },
        method: "GET",
        header: {
          'content-type': 'application/json', // 默认值
          'openId': app.globalData.openId,
          'ts': timestamp,
          'sign': util.sha1("GET" + '/v1/device/snapshots/' + that.data.curDevId + timestamp + app.globalData.openKey)
        },
        success(res) {
          console.log("82:"+JSON.stringify(res.data));
          that.setData({
            'as101': res.data.data[101],
            'as102': res.data.data[102],
            "yali": res.data.data[130],
            "isFD": res.data.data[135],
            'tempVal': res.data.data[142],
            'cnTempVal': res.data.data[143],
            // 'setCnTempVal': res.data.data[104],
            
          })
          if (that.data.isLyTemp){
            if (res.data.data[103] == that.data.setTempVal){
              that.setData({
                'setTempVal': res.data.data[103],
              })
            }else{
              that.setData({
                'setTempVal': that.data.setTempVal,
              })
            }
          }else{
            that.setData({
              'setTempVal': res.data.data[103],
            })
          }
          if (that.data.isCnTemp) {
            if (res.data.data[104] == that.data.setCnTempVal) {
              that.setData({
                'setCnTempVal': res.data.data[104],
              })
            } else {
              that.setData({
                'setCnTempVal': that.data.setCnTempVal,
              })
            }
          } else {
            that.setData({
              'setCnTempVal': res.data.data[104],
            })
          }
          if (res.data.data[136]==1){
            that.setData({
              isWater: true
            })
          }else{
            that.setData({
              isWater: false
            })
          }
          if (res.data.data[129]==1) {
            that.setData({
              isSb: true
            })
          } else {
            that.setData({
              isSb: false
            })
          }
          if (res.data.data[138]==1) {
            that.setData({
              isFire: true
            })
          } else {
            that.setData({
              isFire: false
            })
          }
          if (that.data.as101 == 1) {
            that.setData({
              'as101': 1,
              'powerImg': '../../images/power_sel.png',
              'isPower': true,
            })
          } else {
            that.setData({
              'as101': 0,
              'powerImg': '../../images/power_nor.png',
              'isPower': false,
            })
          }
          if (that.data.as102==1){
            that.setData({
              "modeVal":'冬季',
              'isSummer': false
            })
          }else{
            that.setData({
              "modeVal": '夏季',
              'isSummer': true
            })
          }
          if (res.data.data[128] == 1) {
            that.setData({
              "cnType": '地暖',
              "tempMax":60
            })
          } else {
            that.setData({
              "cnType": '散热片',
              'tempMax': 80
            })
          }
        }
      })
    // }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that=this;
  // 设备快照
    // that.data.setInter1=setInterval(function(){
      // that.devieStatus();//调取获取设备信息的函数
    // },1000);
    var that = this;
    // 设备快照
    if (that.data.offLine) {
      that.data.setInter1 = setInterval(function () {
        that.devListFun();//获取设备列表
        that.devieStatus();//调取获取设备信息的函数
      }, 1000);
    } else {
      that.data.setInter1 = setInterval(function () {
        that.devListFun();//获取设备列表
      }, 1000);
    }
},
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    //清除计时器  即清除setInter
    console.log('onhide');
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearInterval(this.data.setInter1);
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  },
  // 淋浴
  lyBtn:function(){
    this.setData({
      isLy:true
    })
  },
  // 采暖
  cnBtn: function () {
    this.setData({
      isLy: false
    })
  },
  // 下发指令函数
  controlFun: function(par){
    var timestamp = new Date().getTime();
    var param = {
      "as": par
    }
    var params = JSON.stringify(param);
    wx.request({
      url: 'https://papi.zcyun.cn/v1/device/opt/' + this.data.curDevId,
      data: {
        "as": par
      },
      method: "POST",
      header: {
        'content-type': 'application/json', // 默认值
        'openId': app.globalData.openId,
        'ts': timestamp,
        'sign': util.sha1("POST" + '/v1/device/opt/' + this.data.curDevId + params + timestamp + app.globalData.openKey)
      },
      success(res) {
        console.log("231:"+res);
      }
    })
  },
  // 淋浴温度设置
  lySubBtn: function () {
    var that=this;
    var par;
    if(that.data.isPower){
      if (that.data.setTempVal > 30) {
        that.setData({
          isLyTemp: true,
          setTempVal: parseInt(that.data.setTempVal) - 1
        })
        par = {
          "103": parseInt(that.data.setTempVal) - 1
        }
        that.controlFun(par);
      }
    }
  },
  lyAddBtn: function () {
    var that = this;
    var par; //isLyTemp: false,
    if(that.data.isPower){
      if (that.data.setTempVal < 60) {
        that.setData({
          isLyTemp: true,
          setTempVal: parseInt(that.data.setTempVal) + 1
        })
        par = {
          "103": parseInt(that.data.setTempVal) + 1
        }
        that.controlFun(par);
      }
    }
  },
  lyChange: function (e) {
    var that = this;
    that.setData({
      isLyTemp: true,
      setTempVal: e.detail.value
    })
    var par;
      par = {
        "103": e.detail.value
      }
      that.controlFun(par);
  },
  // 采暖温度设置
  cnSubBtn: function () {
    var that = this;
    var par;
    if(that.data.isPower){
      if (that.data.setCnTempVal > 30) {
        that.setData({
          isCnTemp: true,
          setCnTempVal: parseInt(that.data.setCnTempVal) - 1
        })
        par = {
          "104": parseInt(that.data.setCnTempVal) - 1
        }
        that.controlFun(par);
      }
    }
  },
  cnAddBtn: function () {
    var that = this;
    var par;
    if(that.data.isPower){
      if (that.data.setCnTempVal < that.data.tempMax) {
        that.setData({
          isCnTemp: true,
          setCnTempVal: parseInt(that.data.setCnTempVal) + 1
        })
        par = {
          "104": parseInt(that.data.setCnTempVal) + 1
        }
        that.controlFun(par);
      }
    }
  },
  cnChange: function (e) {
    var that = this;
    that.setData({
      isCnTemp: true,
      setCnTempVal: e.detail.value
    })
    var par;
    par = {
      "104": e.detail.value
    }
    that.controlFun(par);
  },
  // 点击开关
  powerBtn:function() {
    var that=this;
    var par;
    if (that.data.as101==0){
      par = {
        "101":1
      }
    }else{
      par = {
        "101": 0
      }
    }
    that.controlFun(par);
  },
  // 模式按键
  modeBtn: function () {
    var that = this;
    var par;
    if (that.data.as101 == 1) {
      that.setData({
        "setMode": true
      })
    }
  },
  summerBtn: function(){
    var that=this;
    var par;
    that.setData({
      "setMode":false
    })
    par = {
      "102": 0
    }
    that.controlFun(par);
  },
  winterBtn: function () {
    var that = this;
    var par;
    that.setData({
      "setMode": false
    })
    par = {
      "102": 1
    }
    that.controlFun(par);
  },
  // 获取设备列表
  devListFun() {
    var that = this;
    var timestamp = new Date().getTime();
    // 查询用户绑定的设备列表以及分组信息
    wx.request({
      url: 'https://papi.zcyun.cn/v1/user/devices',
      method: "GET",
      header: {
        'content-type': 'application/json', // 默认值
        'openId': app.globalData.openId,
        'ts': timestamp,
        'sign': util.sha1("GET" + '/v1/user/devices' + timestamp + app.globalData.openKey)
      },
      success(res) {
        if (res.data.code == 0) {
          var arrDev = res.data.data.devs;
          for (var i = 0; i < arrDev.length; i++) {
            if (that.data.curDevId == arrDev[i].did) {
              if (arrDev[i].online == 1) {
                that.setData({
                  'offLine': false
                })
              } else {
                that.setData({
                  'offLine': true
                })
              }
              return true
            }
          }
        }
      }
    })
  },
})